package com.sh.shanhuo.activity;

import android.app.Activity;
import android.util.Log;

import com.sh.shanhuo.utils.ScreenSwitch;

import java.util.ArrayList;
import java.util.List;


public class ActivityManager {

    private final String TAG = ActivityManager.this.getClass().getSimpleName();

    private List<Activity> activityList = new ArrayList<>();
    private static ActivityManager instance;

    private ActivityManager() {
    }

    // 单例模式中获取唯一的MyApplication实例
    public static ActivityManager getInstance() {
        if (null == instance) {
            instance = new ActivityManager();
        }
        return instance;
    }

    /**
     * 添加activity 到列表
     */
    public void addActivity(Activity activity) {
        if (null != activity) {
            activityList.add(activity);
        }
    }


    /**
     * 从列表中删除activity
     */
    public void removeActivity(Activity activity) {
        if (null != activity) {
            activityList.remove(activity);
        }
    }

    /**
     * 关闭所有的activity
     */
    public void finishAllActivity() {
        for (Activity activity : activityList) {
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
            }
        }
    }


    /*结束界面*/
    public void finishAllActivityExcept(Class<? extends Activity> aClass) {
        for (Activity activity : activityList) {
            if (null != activity && !activity.isFinishing()) {
                if (!aClass.equals(activity.getClass())) {
                    Log.e(TAG, "finish activity " + activity.getClass().getSimpleName());
                    ScreenSwitch.finish(activity);
                }
            }
        }
    }





}
